    //
//  RealityCell.m
//  敢唆嘛!
//
//  Created by lanou on 15/11/23.
//  Copyright © 2015年 zms. All rights reserved.
//

#import "RealityCell.h"
#import "Device.h"
#import "Define.h"

@interface RealityCell()
@property(nonatomic , strong)UIImageView *headerImageView;//头像
@property(nonatomic , strong)UILabel *userNameLabel;//用户名字
@property(nonatomic , strong)UILabel *stateLabel;//发布时间的label
@property(nonatomic , strong)UIView *tagView;//右边标签视图
@property(nonatomic , strong)UIImageView *bigImageView;//最大的ImageView
@property(nonatomic , strong)UILabel *titleLabel;//标题
@property(nonatomic , strong)UILabel *detailLabel;//详情Label
@property(nonatomic , strong)UILabel *recommendLabel;//推荐
@property(nonatomic , strong)UILabel *collectionLabel;//收藏
@property(nonatomic , strong)UILabel *commentsLabel;//评论
@property(nonatomic, strong)UIImageView *tagImageView; // 小火焰
@property(nonatomic, strong)UILabel *tagLabel;          // 标签
@property(nonatomic, strong)UIView *downView;           // 装载三个评论数的view

@end

@implementation RealityCell
{
    CGFloat x;
    CGFloat y;
    CGFloat sx;
    CGFloat sy;
}

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self)
    {
        x = KSWidth / 19;
        y = KSWidth / 19;
        sx = KSWidth / 25;
        sy = KSWidth / 25;
        
//        关闭cell被点击的状态
        self.selectionStyle = NO;
    
        CGFloat width = KSWidth - space*8; //顶部两个label的宽
        CGFloat height = 170;//最大的imageView的高
//        头像
        self.headerImageView = [[UIImageView alloc]initWithFrame:CGRectMake(space, space, KSWidth / 10, KSWidth / 10)];
//        self.headerImageView.backgroundColor = [UIColor redColor];
        
        self.headerImageView.layer.cornerRadius = KSWidth / 20;
        self.headerImageView.layer.masksToBounds = YES;
        [self.contentView addSubview:self.headerImageView];
        
//        用户名字
        self.userNameLabel = [[UILabel alloc]initWithFrame:CGRectMake(space*2+self.headerImageView.frame.size.width, space, width, self.headerImageView.frame.size.height/2)];
        self.userNameLabel.font = [UIFont systemFontOfSize:KSWidth / 28];
//        self.userNameLabel.backgroundColor = [UIColor redColor];
        [self.contentView addSubview:self.userNameLabel];
        
//        发布的时间状态
        self.stateLabel = [[UILabel alloc]initWithFrame:CGRectMake(space*2+self.headerImageView.frame.size.width, space*2.1, width, space)];
        self.stateLabel.alpha = 0.6;
        self.stateLabel.textColor = [UIColor blackColor];
        self.stateLabel.font = [UIFont systemFontOfSize:KSWidth / 34];
        [self.contentView addSubview:self.stateLabel];
        
//        右边标签视图
        self.tagView = [[UIView alloc]initWithFrame:CGRectMake(KSWidth-space*4, space, space*2, space)];
//        self.tagView.backgroundColor = [UIColor redColor];
        [self.contentView addSubview:self.tagView];
//        小火焰
        self.tagImageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, space, space)];
//        self.tagImageView.image = [UIImage imageNamed:@"iconfont-yuanquan"];
//        tagImageView.backgroundColor = [UIColor greenColor];
        [self.tagView addSubview:_tagImageView];
//        标签：真事
        self.tagLabel = [[UILabel alloc]initWithFrame:CGRectMake(space*1.5, 0, space*2, space)];
//        self.tagLabel.text = @"真事";
        self.tagLabel.font = [UIFont systemFontOfSize:11];
//        tagLabel.backgroundColor = [UIColor greenColor];
        [self.tagView addSubview:_tagLabel];
        
//        最大的ImageView
        self.bigImageView = [[UIImageView alloc]initWithFrame:CGRectMake(space, space*4.5, KSWidth - space*2, height)];
//        self.bigImageView.backgroundColor = [UIColor redColor];
        [self.contentView addSubview:self.bigImageView];
        
//        标题
        self.titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(space, space*5+height, KSWidth-space*2, space*2)];
        self.titleLabel.font = [UIFont systemFontOfSize:KSWidth / 20];
//        self.titleLabel.backgroundColor = [UIColor redColor];
        [self.contentView addSubview:self.titleLabel];
        
#warning 详情
//        详情
        self.detailLabel = [[UILabel alloc]initWithFrame:CGRectMake(space, space*7+height, KSWidth-space*2, KSWidth / 20)];
        self.detailLabel.numberOfLines = 6;
        self.detailLabel.font = [UIFont systemFontOfSize:KSWidth / 28];
//        self.detailLabel.backgroundColor = [UIColor redColor];
        [self.contentView addSubview:self.detailLabel];
        
        
        // 创建装载三个评论数的view
        CGFloat infoLabelHeight = self.detailLabel.frame.size.height;
        CGFloat userHeaderImageHeight = self.headerImageView.frame.size.height;
        CGFloat infoTitleLabelHeight = self.titleLabel.frame.size.height;
        CGFloat bigImageViewHeight = self.bigImageView.frame.size.height;
        self.downView = [[UIView alloc] initWithFrame:CGRectMake(KSWidth / 1.7, userHeaderImageHeight + infoTitleLabelHeight + infoLabelHeight + bigImageViewHeight + sy * 3.5, KSWidth - (KSWidth / 2.3) - sy * 4, KSWidth / 29)];
        //        self.downView.backgroundColor = [UIColor redColor];
        [self.contentView addSubview:_downView];
        
        // 创建推荐数图标
        UIImageView *recommendNumberImage = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, KSWidth / 27, KSWidth / 27)];
        recommendNumberImage.image = [UIImage imageNamed:@"iconfont-xunhuan2"];
        [self.downView addSubview:recommendNumberImage];
        
        // 创建推荐数
        CGFloat recommendNumberImageWidth = recommendNumberImage.frame.size.width;
        self.recommendLabel = [[UILabel alloc] initWithFrame:CGRectMake(recommendNumberImageWidth + 5, 0, KSWidth / 14, KSWidth / 27)];
        self.recommendLabel.alpha = 0.4;
        self.recommendLabel.textColor = [UIColor blackColor];
        self.recommendLabel.font = [UIFont systemFontOfSize:KSWidth / 31];
        [self.downView addSubview:_recommendLabel];
        
        // 创建收藏图标
        CGFloat recommendNumberLabelWidth = self.recommendLabel.frame.size.width;
        UIImageView *collectNumberImage = [[UIImageView alloc] initWithFrame:CGRectMake(recommendNumberImageWidth + recommendNumberLabelWidth + sx, 0, KSWidth / 27, KSWidth / 27)];
        collectNumberImage.image = [UIImage imageNamed:@"iconfont-xingxing"];
        [self.downView addSubview:collectNumberImage];
        
        // 创建收藏数
        CGFloat collectNumberImageWidth = collectNumberImage.frame.size.width;
        self.collectionLabel = [[UILabel alloc] initWithFrame:CGRectMake(recommendNumberImageWidth + recommendNumberLabelWidth + collectNumberImageWidth + sx + 5, 0, KSWidth / 14, KSWidth / 27)];
        self.collectionLabel.alpha = 0.4;
        self.collectionLabel.textColor = [UIColor blackColor];
        self.collectionLabel.font = [UIFont systemFontOfSize:KSWidth / 31];
        [self.downView addSubview:_collectionLabel];
        
        // 创建评论图标
        CGFloat collectNumberLabelWidth = self.collectionLabel.frame.size.width;
        UIImageView *commentNumberImage = [[UIImageView alloc] initWithFrame:CGRectMake(recommendNumberImageWidth + recommendNumberLabelWidth + collectNumberImageWidth + collectNumberLabelWidth + sx * 2, 0, KSWidth / 27, KSWidth / 27)];
        commentNumberImage.image = [UIImage imageNamed:@"iconfont-duihuaqipao"];
        [self.downView addSubview:commentNumberImage];
        
        // 创建评论数
        CGFloat commentNumberImageWidth = commentNumberImage.frame.size.width;
        self.commentsLabel = [[UILabel alloc] initWithFrame:CGRectMake(recommendNumberImageWidth + recommendNumberLabelWidth + collectNumberImageWidth + collectNumberLabelWidth + commentNumberImageWidth + sx * 2 + 5, 0, KSWidth / 14, KSWidth / 27)];
        self.commentsLabel.alpha = 0.4;
        self.commentsLabel.textColor = [UIColor blackColor];
        self.commentsLabel.font = [UIFont systemFontOfSize:KSWidth / 31];
        [self.downView addSubview:_commentsLabel];

        
    }
    return  self;
}

// 传值
- (void)setModel:(RealityModel *)model
{
    [self.headerImageView sd_setImageWithURL:[NSURL URLWithString:model.userHeader]];
    self.userNameLabel.text = model.userName;
    self.stateLabel.text = [[PrettyDate alloc] prettyDateWithReference:model.createdAt];
    self.titleLabel.text = model.infoTitle;
    self.recommendLabel.text = [NSString stringWithFormat:@"%ld",model.recommendNumber];
    self.collectionLabel.text = [NSString stringWithFormat:@"%ld",model.collectNumber];
    self.commentsLabel.text = [NSString stringWithFormat:@"%ld",model.commentNumber];
    [self.bigImageView sd_setImageWithURL:[NSURL URLWithString:model.infoImage]];
    self.tagLabel.text = model.type;
    if ([model.type isEqualToString:@"真事"])
    {
        self.tagImageView.image = [UIImage imageNamed:@"iconfont-yuanquan"];
    }
    else if ([model.type isEqualToString:@"创作"])
    {
        self.tagImageView.image = [UIImage imageNamed:@"iconfont-yuanquan-3"];
    }
    else if ([model.type isEqualToString:@"游记"])
    {
        self.tagImageView.image = [UIImage imageNamed:@"iconfont-yuanquan-2"];
    }
    else if ([model.type isEqualToString:@"秘密"])
    {
        self.tagImageView.image = [UIImage imageNamed:@"iconfont-yuanquan-1"];
    }
    
    self.detailLabel.text = model.info;
    
    CGFloat height = 170;
    // 动态返回详情的高度
    CGRect detailLabelBounds = [self.detailLabel.text boundingRectWithSize:CGSizeMake(KSWidth-space*2, (KSHeight - 64) / 5) options:NSStringDrawingUsesLineFragmentOrigin attributes:[NSDictionary dictionaryWithObject:[UIFont systemFontOfSize:KSWidth / 28] forKey:NSFontAttributeName] context:nil];
    
    self.detailLabel.frame = CGRectMake(space, space*7+height, KSWidth-space*2, detailLabelBounds.size.height);
    
    // 计算装载三个评论数的view的动态高度
    self.downView.frame = CGRectMake(KSWidth / 1.7, self.headerImageView.frame.size.height + self.titleLabel.frame.size.height + self.detailLabel.frame.size.height + self.bigImageView.frame.size.height + sy * 3.5, KSWidth - (KSWidth / 2.3) - sy * 4, KSWidth / 29);
    
    //    中间显示省略号，要先截取字符串再加上。。。
//    NSString *string = @"经期过了四天迟迟没有动静，和他调侃道：“如果有了怎么办？”\n“嗯，有点亏，不过我们可以真做了”";
//    NSLog(@"%ld",string.length);
    //    判断字数
//    if (model.info.length > 65)
//    {
//        model.info = [model.info substringToIndex:65];
//        self.detailLabel.text =[NSString stringWithFormat:@"%@...",model.info];
//    }
//    else
//    {
//        self.detailLabel.text = model.info;
//    }
    
    //        设置行间距
//    [self setAttributedString:self.detailLabel line:5];
}

// 动态返回动态cell
+ (CGFloat)heightForCell:(RealityModel *)model
{
    // 动态返回详情的高度
    CGRect detailLabelBounds = [model.info boundingRectWithSize:CGSizeMake(KSWidth-space*2, (KSHeight - 64) / 5) options:NSStringDrawingUsesLineFragmentOrigin attributes:[NSDictionary dictionaryWithObject:[UIFont systemFontOfSize:KSWidth / 28] forKey:NSFontAttributeName] context:nil];
    CGFloat height = detailLabelBounds.size.height + 170 + space * 10;
    return height;
}

//// 设置行间距
//- (void)setAttributedString:(UILabel *)label line:(CGFloat)line
//{
//    //        设置行间距
//    NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:label.text];
//    NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
//    [paragraphStyle setLineSpacing:line];
//    [attributedString addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, label.text.length)];
//    label.attributedText = attributedString;
//}



- (void)awakeFromNib {
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
